// src/pages/shopList/shopList.js
import getIn from '../../../yali-sdk/utils/getIn';
import { SUCCESS } from '../../../yali-sdk/utils/success';
import { FAIL } from '../../../yali-sdk/utils/fail';
import { updateVariableParams, showFail, showSuccess } from '../../config/common';
import fetchShopList from '../../../yali-sdk/business/shop/fetchShopList';
import { cateColor } from '../../../yali-sdk/business/shop/constrants';
import { CATERINNG_GOODS_LIST, HOTEL_GOODS_LIST, POP_SHOP, EXHIBITION_SHOP } from '../pathUrl'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    location:'定位中...',
    shopText: '',
    paddingTop: 0,
    shops:[],
    total: 0,
    shopListParams:{
      page: 1,
      rows: 10,
      cate_id: null,
      order: 'distance',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    const shopText = getIn(options, ['shopText']) || '';
    const cate_id = getIn(options, ['cate_id']);
    let paddingTop = 0;
    const locationText = getApp().globalData.address;
    this.setData({ location: locationText })
    const query = wx.createSelectorQuery().in(this);
    query.select('.header').boundingClientRect((res) => {
      this.setData({ paddingTop: res.height });
    }).exec();
    if (cate_id) {
      const shopListParams = updateVariableParams(this.data.shopListParams, { cate_id: Number(cate_id) });
      this.data.shopListParams = shopListParams;
    };
    this.setData({ shopText });
    wx.showLoading({
      title: '加载中...',
    })
    this.fetchData({ name: shopText });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {
    const page = 1;
    const params = updateVariableParams(this.data.shopListParams, { page });
    this.data.shopListParams = params;
    this.fetchData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {
    const { total, shops } = this.data;
    if (shops.length < total) {
      // 界面加载完, 且还有下一页的情况。
      const pages = getIn(this.data, ['shopListParams', 'page']) || 1;
      const params = updateVariableParams(this.data.shopListParams, { page: pages + 1 });
      this.data.shopListParams = params;
      this.fetchData();
    }
  },
  // 获取店铺列表
  fetchData(param) {
    const app = getApp().globalData;
    const params = {
      ...this.data.shopListParams,
      lng: app.longitude,
      lat: app.latitude,
      name: this.data.shopText,
      ...param
    }
    global.ActionDispatcher.execute(fetchShopList, params, (status, result) => {
      if(status == SUCCESS) {
        const total = getIn(result.data, ['total']);
        let shops = getIn(result.data, ['list']);
        shops = shops.map((x) => {
          x.color = cateColor(x.template);
          return x;
        });
        shops = params.page == 1 ? shops : this.data.shops.concat(shops);
        this.setData({ shops, total, shopListParams: params });
        wx.stopPullDownRefresh();
        wx.hideLoading();
      } else if(status == SUCCESS) {
        wx.stopPullDownRefresh();
        wx.hideLoading();
      }
    })
  },
  // 筛选店铺列表
  selectShop(e) {
    const params = e.detail;
    const newData = {
      page: 1,
      ...params,
    }
    const shopListParams = updateVariableParams(this.data.shopListParams, { ...newData });
    this.data.shopListParams = shopListParams;
    this.fetchData();
  },
  // 输入店铺名称
  onChange(e) {
    this.setData({
      shopText: e.detail.value
    })
  },
  // 点击进入产品列表
  toShop(e) {
    const id = getIn(e.currentTarget, ['id']);
    const cate_id = getIn(e.currentTarget, ['dataset', 'cate_id']);
    const template = getIn(e.currentTarget, ['dataset', 'template']);
    const queryString = `id=${id}&cate_id=${cate_id}`;
    switch (template) {
      case 1: {// 餐饮店铺
        global.navigateHelper.navigateTo({
          pathname: CATERINNG_GOODS_LIST,
          queryString,
        });
        break;
      }
      case 2: {// 酒店店铺
        global.navigateHelper.navigateTo({
          pathname: HOTEL_GOODS_LIST,
          queryString,
        });
        break;
      }
      case 3: {// 展示版
        global.navigateHelper.navigateTo({
          pathname: EXHIBITION_SHOP,
          queryString,
        });
        break;
      }
      default: {// 其他店铺
        global.navigateHelper.navigateTo({
          pathname: POP_SHOP,
          queryString,
        });
        break;
      }
    }
  }
})